package com.wswy.wzcx.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import com.blankj.utilcode.util.SizeUtils;
import com.che.libcommon.api.ApiOptionalResultObserver;
import com.che.libcommon.api.BaseResult;
import com.che.libcommon.recycler.Component;
import com.che.libcommon.recycler.RowFactory;
import com.che.libcommon.recycler.RowListAdapter;
import com.che.libcommon.ui.VOMessage;
import com.che.libcommon.ui.loading.ILoadingView;
import com.che.libcommon.ui.loading.LoadingDelegate;
import com.che.libcommon.ui.loading.OnLoadingShowCallback;
import com.wswy.wzcx.R;
import com.wswy.wzcx.api.Api;
import com.wswy.wzcx.api.ConfigsKt;
import com.wswy.wzcx.model.MyOrderMode;
import com.wswy.wzcx.module.InternalMsgCodeKt;
import com.wswy.wzcx.ui.component.MyOrderComponent;
import com.wswy.wzcx.ui.other.CloseDelegate;
import com.wswy.wzcx.ui.persenter.MyOrderPresenter;

import com.wswy.wzcx.ui.web.WebViewActivity;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangduheng26 on 2018/3/14.
 * wangduheng26@gmail.com
 */

public class MyOrderFragment extends ILoginFragment {
    public static final String TAG = "MyOrderFragment";

    public static MyOrderFragment newInstance() {

        Bundle args = new Bundle();

        MyOrderFragment fragment = new MyOrderFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private MyOrderPresenter presenter;
    private RecyclerView recyclerView;
    private LoadingDelegate mLoadingDelegate;
    private List<MyOrderMode> list;
    RowListAdapter<MyOrderMode> adapter;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        presenter = new MyOrderPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final Context context = getContext();
        presenter.onCreate(context);
        View inflate = inflater.inflate(R.layout.fragment_my_cars, container, false);
        recyclerView = inflate.findViewById(R.id.recycler_view);
        mLoadingDelegate = new LoadingDelegate(inflate, R.id.layout_content, getLoadingConfig());
//        closeDelegate = new CloseDelegate(InternalMsgCodeKt.ORDER_STATUS_CHANGED, null,
//            new CloseCallback() {
//                @Override
//                public void call() {
//                    setupPage();
//                }
//            });

        //联系客服悬浮按钮
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(R.drawable.float_wechatservice);

        FrameLayout.LayoutParams lp = new LayoutParams(SizeUtils.dp2px(60),SizeUtils.dp2px(60));
        lp.gravity = Gravity.RIGHT|Gravity.BOTTOM;

        lp.bottomMargin = SizeUtils.dp2px(128);

        ((ViewGroup) inflate).addView(imageView,lp);

        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.startWebview(getContext(), ConfigsKt.H5_CUSTOMER_SERVICE());
            }
        });

        return inflate;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Context context = getContext();
        list = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setHasFixedSize(true);
        assert context != null;
        adapter = new RowListAdapter<>(MyOrderMode.class, new RowFactory<MyOrderMode>() {
            @Override
            public Component<MyOrderMode> create(ViewGroup parent) {
                return new MyOrderComponent(parent);
            }
        });
        presenter.setAdapterClick(adapter);
        recyclerView.setAdapter(adapter);

    }

    CloseDelegate closeDelegate;

    private ILoadingView.LoadingConfigure getLoadingConfig() {
        return new ILoadingView.LoadingConfigure() {
            @Override
            public VOMessage getEmptyMessage() {
                return VOMessage.createEmpty(0, R.string.empty_order, 0);
            }

            @Override
            public View.OnClickListener getOnEmptyClickClickListener() {
                return null;
            }

            @Override
            public View.OnClickListener getOnRetryClickListener() {
                return new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setupPage();
                    }
                };
            }

            @Override
            public OnLoadingShowCallback getOnLoadingShowCallback() {
                return null;
            }
        };
    }

    @Override
    public void setupPage() {

        Api.get().fetchOrderList().subscribe(new ApiOptionalResultObserver<List<MyOrderMode>>() {
            @Override
            protected void onApiResult(@Nullable List<MyOrderMode> data) {
                if (data == null || data.isEmpty()) {
                    mLoadingDelegate.showEmpty();
                } else {
                    list.clear();
                    list.addAll(data);
                    adapter.setItems(list);
                    adapter.notifyDataSetChanged();
                    mLoadingDelegate.showData();
                }

            }

            @Override
            protected void onApiError(@Nullable BaseResult result) {
                mLoadingDelegate.showError(result);
            }

            @Override
            protected void onStart() {
                super.onStart();
                mLoadingDelegate.showLoading(true);
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null)
            presenter.onDestroy();
        if (closeDelegate != null)
            closeDelegate.onDestroy();
    }

    @Override
    public String getFragmentTitle() {
        return "我的订单";
    }
}
